package com.GamersCom.games;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.GamersCom.db.Genre;
import com.GamersCom.model.GenreMgr;
import com.GamersCom.model.MgrFactory;
import com.GamersCom.util.GlobalCONST;

/**
 * Servlet implementation class AdminGenre
 */
public class AdminGenre extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		process(request,response);
//		HttpSession session = request.getSession();
//		String nextView = "/genre.jsp";
//		
//		MgrFactory factory = (MgrFactory) this.getServletContext()
//				.getAttribute("mgrFactory");
//		GenreMgr genreMgr = factory.getGenreMgr();
//		
//		// Get the platform List
//		List<Genre> list;
//		try
//		{
//			list = genreMgr.getAll();
//			request.setAttribute("genreList", list);
//		}
//		catch (SQLException e)
//		{
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			request.setAttribute("msg", "Error Retrieve Genre list!");
//		}
//
//		request.getRequestDispatcher(nextView).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		process(request, response);
	}

	protected void process(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		String nextView = "/genre.jsp";
		String username = (String) session.getAttribute("username");

		// redirect the user to login
		if (username == null)
		{
			nextView = "/login.jsp";
		}
		else
		{
			int group = (Integer) session.getAttribute("accesslevel");

			if (group != GlobalCONST.ADMINISTRATOR)
			{
				request.setAttribute("error", "Restricted Area!");
				nextView = "/result.jsp";

			}
			else
			{
				MgrFactory factory = (MgrFactory) this.getServletContext()
						.getAttribute("mgrFactory");
				GenreMgr genreMgr = factory.getGenreMgr();

				// pasing the action
				String submit = request.getParameter("submit");

				if (submit != null)
				{

					// Add Action
					if (submit.equals("Add"))
					{
						// retrieve the form data
						Genre genre = new Genre();
						genre.setName(request.getParameter("genre"));

						if (genreMgr.nameExists(genre.getName()))
						{
							request.setAttribute("msg", "Fail: Name exists!!");
						}
						else
						{

							try
							{
								genreMgr.insert(genre);
								request.setAttribute("msg", "Add successfully!");
							}
							catch (SQLException e)
							{
								// TODO Auto-generated catch block
								e.printStackTrace();
								request.setAttribute("msg", "Add failed!");
							}
						}
					}

					// Delete Action
					if (submit.equals("Delete"))
					{
						Genre genre = new Genre();
						genre.setName(request.getParameter("genre"));

						try
						{
							genreMgr.delete(genre);
							request.setAttribute("msg", "Delete successfully!");
						}
						catch (SQLException e)
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
							request.setAttribute("msg", "Error: Delete failed!");
						}
					}

					// Update Action
					if (submit.equals("Update"))
					{
						Genre genre = new Genre();
						genre.setName(request.getParameter("genre"));
						Genre genreNew = new Genre();
						genreNew.setName(request.getParameter("genreNew"));

						if (  (! genre.getName().toLowerCase().equals(genreNew.getName().toLowerCase()) )   && genreMgr.nameExists(genreNew.getName()))
						{
							request.setAttribute("msg", "Fail: Name exists!!");
						}
						else
						{
							try
							{
								genreMgr.update(genre, genreNew);
								request.setAttribute("msg",
										"Update successfully!");
							}
							catch (SQLException e)
							{
								// TODO Auto-generated catch block
								e.printStackTrace();
								request.setAttribute("msg",
										"Error: Update failed!");
							}
						}

					}

				}

				// Get the platform List
				List<Genre> list;
				try
				{
					list = genreMgr.getAll();
					request.setAttribute("genreList", list);
				}
				catch (SQLException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
					request.setAttribute("msg", "Error Retrieve Genre list!");
				}

			}

		}

		request.getRequestDispatcher(nextView).forward(request, response);
	}

}
